﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
namespace BYManageInfoSystem
{
    public partial class Frm_SendCountryAndPort : DockContent
    {
        private DGBY.BLL.ZYHarborTab m_bllZYHarborTab = new DGBY.BLL.ZYHarborTab();
        private DataSet m_ds = new DataSet();
        private int m_nSelectPortID = 0;
        private DGBY.BLL.QYCountryTab m_bllQYCountryTab = new DGBY.BLL.QYCountryTab();
        private DataSet m_dsCountry = new DataSet();
        private int m_nSelectCountryID = 0;

        public Frm_SendCountryAndPort()
        {
            InitializeComponent();
        }

        private void ShowAllRecordInDataGridView()
        {
            dataGridViewPortList.DataSource = null;
            m_ds = m_bllZYHarborTab.GetAllList();
            if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            {
                dataGridViewPortList.DataSource = m_ds.Tables[0];
                dataGridViewPortList.Columns[0].Visible = false;
            }
        }

        private void buttonShowAllRecordPort_Click(object sender, EventArgs e)
        {
            ShowAllRecordInDataGridView();
        }

        private void dataGridViewPortList_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string strPortID = this.dataGridViewPortList.Rows[this.dataGridViewPortList.CurrentCell.RowIndex].Cells[0].Value.ToString();
            m_nSelectPortID = Convert.ToInt32(strPortID, 10);
            cellclick();
        }

        private void cellclick()
        {
            DGBY.Model.ZYHarborTab modelZYHarborTab = m_bllZYHarborTab.GetModel(m_nSelectPortID);
            if (modelZYHarborTab != null)
            {
                this.textBoxPort.Text = modelZYHarborTab.ZYHarborName;
                this.textBoxPortEng.Text = modelZYHarborTab.ZYHarborNameEN;
            }
            else
            {
                this.textBoxPort.Text = "";
                this.textBoxPortEng.Text = "";
            }
        }

        private void dataGridViewPortList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            {
                using (SolidBrush b = new SolidBrush(dataGridViewPortList.RowHeadersDefaultCellStyle.ForeColor))
                {
                    e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 10, e.RowBounds.Location.Y + 4);
                }
            }
        }

        private void buttonAddPort_Click(object sender, EventArgs e)
        {
            try
            {
                DGBY.Model.ZYHarborTab modelZYHarborTab = new DGBY.Model.ZYHarborTab();
                modelZYHarborTab.ZYHarborName = this.textBoxPort.Text;
                modelZYHarborTab.ZYHarborNameEN = this.textBoxPortEng.Text;

                int nResult = m_bllZYHarborTab.Add(modelZYHarborTab);

                if (nResult != 1)
                {
                    Frm_Main.Log2DB(this.Text, "增加装运港", modelZYHarborTab.ZYHarborName);

                    m_nSelectPortID = m_bllZYHarborTab.GetMaxId() - 1;
                    ShowAllRecordInDataGridView();
                    //MessageBox.Show("数据添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
         
        }

        private void buttonModifyPort_Click(object sender, EventArgs e)
        {
            try
            {
                if (m_bllZYHarborTab.GetModel(m_nSelectPortID) == null)
                {
                    MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                DGBY.Model.ZYHarborTab modelZYHarborTab = m_bllZYHarborTab.GetModel(m_nSelectPortID);
                modelZYHarborTab.ZYHarborName = this.textBoxPort.Text;
                modelZYHarborTab.ZYHarborNameEN = this.textBoxPortEng.Text;
                //modelZYHarborTab.ZYHarborTabID = m_nSelectPortID;

                m_bllZYHarborTab.Update(modelZYHarborTab);

                Frm_Main.Log2DB(this.Text, "修改装运港", modelZYHarborTab.ZYHarborName);
                ShowAllRecordInDataGridView();
                MessageBox.Show("数据更新成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
         
        }

        private void buttonDelPort_Click(object sender, EventArgs e)
        {
            if (m_bllZYHarborTab.GetModel(m_nSelectPortID) == null)
            {
                MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("确实要删除该记录么，删除该记录可能会连带删除其他表记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            m_bllZYHarborTab.Delete(m_nSelectPortID);
            ShowAllRecordInDataGridView();

            MessageBox.Show("数据删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            cellclick();
        }

        private void ShowAllCountryRecordInDataGridView()
        {
            dataGridViewCountryList.DataSource = null;
            m_dsCountry = m_bllQYCountryTab.GetAllList();
            if (m_dsCountry.Tables.Count > 0 && m_dsCountry.Tables[0].Rows.Count > 0)
            {
                dataGridViewCountryList.DataSource = m_dsCountry.Tables[0];
                dataGridViewCountryList.Columns[0].Visible = false;
            }
        }

        private void buttonCountryShowAllRecords_Click(object sender, EventArgs e)
        {
            ShowAllCountryRecordInDataGridView();
        }

        private void dataGridViewCountryList_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string strCountryID = this.dataGridViewCountryList.Rows[this.dataGridViewCountryList.CurrentCell.RowIndex].Cells[0].Value.ToString();
            m_nSelectCountryID = Convert.ToInt32(strCountryID, 10);
            cellclickCountry();
        }

        private void cellclickCountry()
        {
            DGBY.Model.QYCountryTab modelQYCountryTab = m_bllQYCountryTab.GetModel(m_nSelectCountryID);
            if (modelQYCountryTab != null)
            {
                this.textBoxCountry.Text = modelQYCountryTab.CountryName;
            }
            else
            {
                this.textBoxCountry.Text = "";
            }
        }

        private void dataGridViewCountryList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            {
                using (SolidBrush b = new SolidBrush(dataGridViewCountryList.RowHeadersDefaultCellStyle.ForeColor))
                {
                    e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 10, e.RowBounds.Location.Y + 4);
                }
            }
        }

        private void buttonAddCountry_Click(object sender, EventArgs e)
        {
            try
            {
                DGBY.Model.QYCountryTab modelQYCountryTab = new DGBY.Model.QYCountryTab();
                modelQYCountryTab.CountryName = this.textBoxCountry.Text;


                int nResult = m_bllQYCountryTab.Add(modelQYCountryTab);

                if (nResult != 1)
                {
                    Frm_Main.Log2DB(this.Text, "增加起运国", modelQYCountryTab.CountryName);

                    m_nSelectCountryID = m_bllQYCountryTab.GetMaxId() - 1;
                    ShowAllCountryRecordInDataGridView();
                    //MessageBox.Show("数据添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
         
        }

        private void buttonModifyCountry_Click(object sender, EventArgs e)
        {
            try
            {
                if (m_bllQYCountryTab.GetModel(m_nSelectCountryID) == null)
                {
                    MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                DGBY.Model.QYCountryTab modelQYCountryTab = m_bllQYCountryTab.GetModel(m_nSelectCountryID);
                modelQYCountryTab.CountryName = this.textBoxCountry.Text;
                //modelQYCountryTab.QYCountryTabID = m_nSelectCountryID;

                m_bllQYCountryTab.Update(modelQYCountryTab);

                Frm_Main.Log2DB(this.Text, "修改起运国", modelQYCountryTab.CountryName);
                ShowAllCountryRecordInDataGridView();
                MessageBox.Show("数据更新成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
          
        }

        private void buttonDelCountry_Click(object sender, EventArgs e)
        {
            if (m_bllQYCountryTab.GetModel(m_nSelectCountryID) == null)
            {
                MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("确实要删除该记录么，删除该记录可能会连带删除其他表记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            m_bllQYCountryTab.Delete(m_nSelectCountryID);
            ShowAllCountryRecordInDataGridView();

            MessageBox.Show("数据删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            cellclickCountry();
        }

        private void Frm_SendCountryAndPort_Load(object sender, EventArgs e)
        {
            if (Frm_Main.m_strLoginUser == "管理员")
            {
                this.buttonDelCountry.Visible = true;
                this.buttonDelPort.Visible = true;
            }
        }
    }
}